package com.xuecheng.lucene;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class LuceneIndexData {
    @Test
    public void creataIndexData() throws IOException {
//        1.创建一个文件夹Directory，指定索引存放位置
        Directory directory = FSDirectory.open(new File("D:\\IO\\lucene").toPath());
//        2.基于Directory，创建一个写入索引indexwriter
        IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig());
//        3.读取磁盘文件，为每一个文件创建文件对象
        File dir = new File("D:\\IO\\text");
        File[] files = dir.listFiles();
        for (File f : files
        ) {
//            获取文件名
            String name = f.getName();
//            获取文件目录
            String path = f.getPath();
//            获取文件内容
            String content = FileUtils.readFileToString(f, "utf-8");
//            获取文件大小
            long size = FileUtils.sizeOf(f);
//        4.遍历文件对象，获取文件文件，创建索引库的域
            Field fieldname = new TextField("filedname", name, Field.Store.YES);
            Field fieldDir = new TextField("filedPath", path, Field.Store.YES);
            Field fieldcontent = new TextField("filedcontent", content, Field.Store.YES);
            Field fieldSize = new TextField("fieldSize", size + "", Field.Store.YES);
//            创建文档对象，像文档对象添加域
            Document document = new Document();
            document.add(fieldname);
            document.add(fieldDir);
            document.add(fieldSize);
            document.add(fieldcontent);
//        5.将域写入索引库
            indexWriter.addDocument(document);
        }
//        关闭写出
        indexWriter.close();
    }

    @Test
    public void searchData() throws IOException {
//        1.指定索引库位置
        Directory directory = FSDirectory.open(new File("D:\\IO\\lucene").toPath());
//        2.创建索引 IndexReader
        IndexReader indexReader = DirectoryReader.open(directory);
//        3.创建搜索 indexSearcher
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//        4.创建一个query对象
        Query query = new TermQuery(new Term("filedcontent", "apache"));
//        5.执行查询，得到一个topdcs
        TopDocs topDocs = indexSearcher.search(query, 5);
//        6获取总记录数
        int totalHits = topDocs.totalHits;
        System.out.println(totalHits);
//        7获取文档列表
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//        8遍历获取内容
        for (ScoreDoc s : scoreDocs
        ) {
//            获取文档id
            int doc = s.doc;
//            根据文档获取文档对象
            Document doc1 = indexSearcher.doc(doc);
//            获取文档域
            String filedname = doc1.get("filedname");
            String filedPath = doc1.get("filedPath");
            String filedcontent = doc1.get("filedcontent");
            String fieldSize = doc1.get("fieldSize");
            System.out.println(filedname);
            System.out.println(filedPath);
            System.out.println(filedcontent);
            System.out.println(fieldSize);
            System.out.println("+++++++++++++++++++++流浪的分割线+++++++++++++++++++");
        }
        indexReader.close();
//        9关闭indexreader
    }

    /**
     * @Description: 添加文档
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @Test
    public void addDocument() throws IOException {
//        1.创建一个写出索引
        IndexWriter indexWriter = new IndexWriter(FSDirectory.open(new File("D:\\IO\\lucene").toPath()), new IndexWriterConfig());
//        2创建文档
        Document document = new Document();
//        3向文档的域中写入值
        File file = new File("D:\\IO\\text\\apache.txt");
        String name = file.getName();
        String path = file.getPath();
        String content = FileUtils.readFileToString(file);
        document.add(new TextField("fieldname", name, Field.Store.YES));
        document.add(new TextField("fieldcontent", content, Field.Store.YES));
        document.add(new StoredField("fieldpath", path));

        indexWriter.addDocument(document);
        indexWriter.close();
    }

    @Test
    public void deleteIndex() throws IOException {
        //        1.创建一个写出索引
        IndexWriter indexWriter = new IndexWriter(FSDirectory.open(new File("D:\\IO\\lucene").toPath()), new IndexWriterConfig());
        indexWriter.deleteAll();
        indexWriter.close();
    }
}
